package com.flyduck.modules.schedule.web;

import com.flyduck.common.annotation.Log;
import com.flyduck.common.vo.AjaxResult;
import com.flyduck.common.vo.PageResult;
import com.flyduck.common.vo.QueryVo;
import com.flyduck.modules.schedule.domain.SysScheduleJob;
import com.flyduck.modules.schedule.service.SysScheduleJobService;
import com.flyduck.modules.system.domain.SysJob;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @program: flyduck-admin
 * @description: 定时任务
 * @author: flyduck
 * @create: 2020-06-16 15:47
 **/
@Controller
@RequestMapping("/schedule")
public class SysScheduleJobController {

    @Autowired
    private SysScheduleJobService sysScheduleJobService;

    /**
     * 首页
     * @return
     */
    @Log(value = "跳转到定时任务页面")
    @RequiresPermissions("schedule:list")
    @GetMapping
    public String index(){
        return "monitor/schedule";
    }


    /**
     * 分页数据
     * @return
     */
    @Log(value = "定时任务页面数据查询")
    @RequiresPermissions("schedule:list")
    @PostMapping("/list")
    @ResponseBody
    public PageResult findPage(QueryVo queryVo){
        return sysScheduleJobService.findPage(queryVo);
    }


    /**
     * 保存定时任务
     * @param sysScheduleJob
     * @return
     */
    @Log(value = "保存定时任务")
    @RequiresPermissions("schedule:add")
    @GetMapping("save")
    @ResponseBody
    public AjaxResult save(SysScheduleJob sysScheduleJob){
        AjaxResult ajaxResult = new AjaxResult();
        try {
            sysScheduleJobService.save(sysScheduleJob);
            ajaxResult.setSuccess(true);
            ajaxResult.setMsg("保存成功");
        } catch (Exception e) {
            e.printStackTrace();
            ajaxResult.setSuccess(false);
            ajaxResult.setMsg("保存失败");
        }
        return ajaxResult;
    }


    /**
     * 更新定时任务
     * @param sysScheduleJob
     * @return
     */
    @Log(value = "更新定时任务")
    @RequiresPermissions("schedule:edit")
    @GetMapping("update")
    @ResponseBody
    public AjaxResult update(SysScheduleJob sysScheduleJob){
        AjaxResult ajaxResult = new AjaxResult();
        try {
            sysScheduleJobService.update(sysScheduleJob);
            ajaxResult.setSuccess(true);
            ajaxResult.setMsg("更新成功");
        } catch (Exception e) {
            e.printStackTrace();
            ajaxResult.setSuccess(false);
            ajaxResult.setMsg("更新失败");
        }
        return ajaxResult;
    }

    /**
     * 根据定时任务id删除定时任务
     * @param id
     * @return
     */
    @Log(value = "根据定时任务id删除定时任务")
    @RequiresPermissions("schedule:del")
    @PostMapping("delete")
    @ResponseBody
    public AjaxResult delete(Long id){
        AjaxResult ajaxResult = new AjaxResult();
        try {
            sysScheduleJobService.deleteById(id);
            ajaxResult.setSuccess(true);
            ajaxResult.setMsg("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            ajaxResult.setSuccess(false);
            ajaxResult.setMsg("删除失败");
        }
        return ajaxResult;
    }

    /**
     * 立即执行定时任务
     * @param id
     * @return
     */
    @Log(value = "立即执行定时任务")
    @RequiresPermissions("schedule:run")
    @PostMapping("run")
    @ResponseBody
    public AjaxResult run(Long id){
        AjaxResult ajaxResult = new AjaxResult();
        try {
            sysScheduleJobService.run(id);
            ajaxResult.setSuccess(true);
            ajaxResult.setMsg("执行任务成功");
        } catch (Exception e) {
            e.printStackTrace();
            ajaxResult.setSuccess(false);
            ajaxResult.setMsg("执行任务失败");
        }
        return ajaxResult;
    }

    /**
     * 暂停定时任务
     * @param id
     * @return
     */
    @Log(value = "暂停定时任务")
    @RequiresPermissions("schedule:pause")
    @PostMapping("pause")
    @ResponseBody
    public AjaxResult pause(Long id){
        AjaxResult ajaxResult = new AjaxResult();
        try {
            sysScheduleJobService.pause(id);
            ajaxResult.setSuccess(true);
            ajaxResult.setMsg("暂停定时任务成功");
        } catch (Exception e) {
            e.printStackTrace();
            ajaxResult.setSuccess(false);
            ajaxResult.setMsg("暂停定时任务失败");
        }
        return ajaxResult;
    }

    /**
     * 恢复定时任务
     * @param id
     * @return
     */
    @Log(value = "恢复定时任务")
    @RequiresPermissions("schedule:resume")
    @PostMapping("resume")
    @ResponseBody
    public AjaxResult resume(Long id){
        AjaxResult ajaxResult = new AjaxResult();
        try {
            sysScheduleJobService.resume(id);
            ajaxResult.setSuccess(true);
            ajaxResult.setMsg("恢复定时任务成功");
        } catch (Exception e) {
            e.printStackTrace();
            ajaxResult.setSuccess(false);
            ajaxResult.setMsg("恢复定时任务失败");
        }
        return ajaxResult;
    }

    /**
     * 根据id查询定时任务
     * @param id
     * @return
     */
    @Log(value = "根据id查询定时任务")
    @GetMapping("getScheduleById")
    @ResponseBody
    public SysScheduleJob getScheduleById(Long id){
        return sysScheduleJobService.getScheduleById(id);
    }



}
